package com.systemCapacity.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.systemCapacity.common.dto.ActivityPerformanceRequestDto;
import com.systemCapacity.common.dto.ActivityResourceRequestDto;
import com.systemCapacity.entity.ActivityResourceRequest;
import com.systemCapacity.mapper.ActivityResourceMapper;
import com.systemCapacity.service.ActivityPerformanceService;
import com.systemCapacity.service.ActivityResourceService;
import com.systemCapacity.service.BattleCapacityService;
import com.systemCapacity.service.EquipmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

@Service
public class ActivityResourceServiceImpl extends ServiceImpl<ActivityResourceMapper, ActivityResourceRequest>
        implements ActivityResourceService {

    @Autowired
    ActivityPerformanceService activityPerformanceService;

    @Autowired
    ActivityResourceMapper activityResourceMapper;

    @Autowired
    BattleCapacityService battleCapacityService;

    @Autowired
    EquipmentService equipmentService;

    @Override
    public List<ActivityResourceRequestDto> getResourceRequests(String activityId, boolean totalPath) {
        QueryWrapper<ActivityResourceRequest> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("activityId", activityId);
        queryWrapper.orderByAsc("requestNum");
        List<ActivityResourceRequest> resourceRequests = activityResourceMapper.selectList(queryWrapper);
        List<ActivityResourceRequestDto> dtos = resourceRequests.stream().map(
                activityResourceRequest -> BeanUtil.copyProperties(activityResourceRequest, ActivityResourceRequestDto.class)
        ).collect(Collectors.toList());
        dtos.forEach(dto -> {
            dto.setCapacityName(battleCapacityService.getById(dto.getCapacityId()).getCapacityName());
            dto.setEquipmentName(equipmentService.getById(dto.getEquipmentId()).getEquipmentName());
            dto.setPerformanceRequests(activityPerformanceService.getPerformances(dto.getRequestId()));
            dto.setSatisfiedRequestsNum(dto.getPerformanceRequests().stream().filter(ActivityPerformanceRequestDto::isSatisfied).count());
        });
        return dtos;
    }

    @Override
    @Transactional
    public boolean saveResourceRequest(ActivityResourceRequestDto dto, String activityId) {
        ActivityResourceRequest activityResourceRequest = new ActivityResourceRequest();
        activityResourceRequest.setActivityId(activityId);
        activityResourceRequest.setRequestId(UUID.randomUUID().toString());
        activityResourceRequest.setRequestNum(dto.getRequestNum());
        activityResourceRequest.setCapacityId(dto.getCapacityId());
        activityResourceRequest.setEquipmentId(dto.getEquipmentId());
        save(activityResourceRequest);
        List<ActivityPerformanceRequestDto> performanceRequests = dto.getPerformanceRequests();
        performanceRequests.forEach(request ->
                activityPerformanceService.savePerformances(request, activityResourceRequest.getRequestId()));
        return true;
    }

    /**
     * 删除作战活动下全部作战任务
     * @param activityId 作战活动id
     * @return 操作是否成功
     */
    @Override
    public boolean deleteResourceRequestByActivityId(String activityId) {
        QueryWrapper<ActivityResourceRequest> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("activityId", activityId);
        List<ActivityResourceRequest> requests = activityResourceMapper.selectList(queryWrapper);
        requests.forEach(request -> activityPerformanceService.deletePerformanceByPid(request.getRequestId()));
        return remove(queryWrapper);
    }

    /**
     * 查看作战资源配置中装备相关联的能力
     * @param capacityId 能力id
     * @return 相关联的装备列表
     */
    @Override
    public List<String> getRelatedEquipmentId(String capacityId) {
        QueryWrapper<ActivityResourceRequest> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("capacityId", capacityId);
        queryWrapper.select("DISTINCT equipmentId");
        return activityResourceMapper.selectList(queryWrapper).stream()
                .map(ActivityResourceRequest::getEquipmentId).collect(Collectors.toList());
    }

    /**
     * 查询作战活动关联的装备
     * @param activityId 作战活动id
     * @return 装备id
     */
    @Override
    public List<String> getActivityEquipment(String activityId) {
        QueryWrapper<ActivityResourceRequest> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("activityId", activityId);
        return activityResourceMapper.selectList(queryWrapper).stream()
                .map(ActivityResourceRequest::getEquipmentId).collect(Collectors.toList());
    }

    @Override
    public List<String> getRelatedActivities(String capacityId) {
        QueryWrapper<ActivityResourceRequest> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("capacityId", capacityId);
        return activityResourceMapper.selectList(queryWrapper).stream().map(ActivityResourceRequest::getActivityId).collect(Collectors.toList());
    }
}
